package com.yz.app.sale.view.fragment;


import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.yz.app.sale.R;
import com.yz.app.sale.base.BaseMvpFragment;
import com.yz.app.sale.entity.ArticleEntity;
import com.yz.app.sale.entity.BannerEntity;
import com.yz.app.sale.presenter.FindPresenter;
import com.yz.app.sale.view.adapter.ArticleAdapter;
import com.yz.app.sale.view.impl.FindViewImpl;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * ScollView   嵌套
 * 1. 内容显示不全
 * 2. 事件冲突
 * ListView复用失效
 */
public class FindFragment extends BaseMvpFragment<FindViewImpl, FindPresenter> implements FindViewImpl,XRecyclerView.LoadingListener {
    HeadViewHolder hvh;
    @BindView(R.id.find_xrv)
    XRecyclerView findXrv;

    private ArrayList<ArticleEntity> articleList = new ArrayList<>();


    public static FindFragment newInstance() {
        return new FindFragment();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_find;
    }

    @Override
    public void initView() {
        //头部View辅助类

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        findXrv.setLayoutManager(layoutManager);

        //设置刷新的样式
        findXrv.setRefreshProgressStyle(ProgressStyle.SquareSpin);
        //设置自动刷新
        findXrv.setLoadingListener(this);
        findXrv.setAdapter(new ArticleAdapter(context, R.layout.adapter_article_layout));
        hvh = new HeadViewHolder();
        //rcv添加头部View
        findXrv.addHeaderView(hvh.headView);
        findXrv.setRefreshing(true);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onRefresh() {
        presenter.getFindHomeData();
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    protected FindPresenter initPresenter() {
        return new FindPresenter();
    }

    @Override
    public void onSuccess() {

    }

    @Override
    public void onFaile() {

    }


    class HeadViewHolder {
        public View headView;
        @BindView(R.id.find_head_banner_covb)
        ConvenientBanner covb;
        ArrayList<BannerEntity> bannerList = new ArrayList<>();

        public HeadViewHolder() {
            this.headView = inflater.inflate(R.layout.find_head_layout, findXrv, false);
            ButterKnife.bind(this, headView);
            initBanner();
        }

        private void initBanner() {
            covb.setPages(new BannerCBViewHolderCreator(), bannerList)
                    .setPageIndicator(new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher})
                    .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_LEFT);
        }

        private void initData() {


        }


    }

    private class BannerCBViewHolderCreator implements CBViewHolderCreator<ImageHolder> {
        @Override
        public ImageHolder createHolder() {
            return new ImageHolder();
        }
    }

    public class ImageHolder implements Holder<BannerEntity> {

        private ImageView img;

        @Override
        public View createView(Context context) {
            img = new ImageView(context);
            return img;
        }

        @Override
        public void UpdateUI(Context context, int position, BannerEntity data) {

        }
    }

}
